package com.wyhello.tube4.spring.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.wyhello.tube4.util.IniReader;
import com.wyhello.tube4.util.MD5Util;

public class MD5Interceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		//获取绝对路径
		String path = request.getSession().getServletContext().getRealPath("");
		//获取key
		String key = IniReader.readValue(path+"/WEB-INF/classes/key.ini", "key");
		//获取code
		String code = IniReader.readValue(path+"/WEB-INF/classes/key.ini", "code");
		
		/*System.out.println(key);
		System.out.println(code);*/
		
		if (MD5Util.validPassword(key, code)) {
			return true;
		} else {
			JOptionPane.showMessageDialog(null, "软件激活码有误");
			response.sendRedirect("/Tube4/login.jsp");
			return false;
		}
	}

}
